<template>
  <el-form
    ref="loginForm"
    :model="loginUser"
    :rules="rules"
    label-width="100px"
    class="loginForm sign-in-form"
  >
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="loginUser.email" placeholder="输入邮箱"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="loginUser.password"
        type="password"
        placeholder="输入密码"
      ></el-input>
    </el-form-item>

    <el-form-item>
      <el-button
        @click="handleLogin('loginForm')"
        type="primary"
        class="submit-btn"
        >登陆</el-button
      >
    </el-form-item>

    <!-- 找回密码 -->
    <div class="tiparea">
      <p>忘记密码？ <a>立即找回</a></p>
    </div>
  </el-form>
</template>

<script>
import { loginUser, rules } from "@/utils/LoginValidator";
export default {
  data() {
    return { loginUser, rules };
  },
  methods: {
    handleLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/api/login", this.loginUser).then((res) => {
            if (res.data.success) {
              this.$store.commit("setuser_info",res.data.user_info)
              this.$message.success("登陆成功");
              this.$router.replace("/");
            }else{
              this.$message.warning(res.data.msg);
            }
          });
          // this.$message.success("登陆");
        } else {
          this.$message.warning("输入错误！");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
/* form */
.loginForm {
  margin-top: 40px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 15px #ddd;
}

.submit-btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p a {
  color: #409eff;
}
</style>
